package admin

import (
	"anban/service"
	"anban/utils"
	"github.com/astaxie/beego"
	"html/template"
	"strconv"
)

type ArticleController struct {
	BaseController
}

func (c *ArticleController) Add() {
	p := map[string]interface{}{}
	_, articleTypeList := service.GetArticleTypeList(p)
	c.Data["articleTypeList"] = articleTypeList
	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "admin/article/add.html"
}

func (c *ArticleController) DoAdd() {
	var res *utils.ResJsonStruct
	input := c.Input()
	if  len(input["article_type_id"][0]) <= 0 {
		res = utils.ResJson(0, "请选择文章分类", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if len(input["title"][0]) <= 0 {
		res = utils.ResJson(0, "请输入文章标题", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if len(input["content"][0]) <= 0 {
		res = utils.ResJson(0, "文章内容过少", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	_, err := service.AddArticle(input)
	if err != nil {
		res = utils.ResJson(0, "添加失败", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	res = utils.ResJson(1, "添加成功", "")
	c.Data["json"] = res
	c.ServeJSON()
}

func (c *ArticleController) Edit() {
	id := utils.Atoi64(c.Ctx.Input.Param(":id"))
	p := map[string]interface{}{}
	p["id"] = id
	p["convert"] = true
	record := service.GetArticleInfo(p)
	c.Data["record"] = record
	p = map[string]interface{}{}
	_, articleTypeList := service.GetArticleTypeList(p)
	c.Data["articleTypeList"] = articleTypeList
	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "admin/article/edit.html"
}

func (c *ArticleController) DoEdit() {
	var res *utils.ResJsonStruct
	input := c.Input()
	if  len(input["article_type_id"][0]) <= 0 {
		res = utils.ResJson(0, "请选择文章分类", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if len(input["title"][0]) <= 0 {
		res = utils.ResJson(0, "请输入文章标题", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if len(input["content"][0]) <= 0 {
		res = utils.ResJson(0, "文章内容过少", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	_, err := service.EditArticle(input)
	if err != nil {
		res = utils.ResJson(0, "修改失败", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	res = utils.ResJson(1, "修改成功", "")
	c.Data["json"] = res
	c.ServeJSON()
}

func (c *ArticleController) ShowList() {
	curPage, _ := strconv.Atoi(c.GetString("p"))
	if curPage <= 0 {
		curPage = 1
	}
	perCount, _ := beego.AppConfig.Int("percount")
	symPageCount, _ := beego.AppConfig.Int("symmetricpagecount")
	p := map[string]interface{}{}
	p["relation"] = true
	articleTypeId := c.GetString("article_type_id")
	if len(articleTypeId) > 0 {
		p["articleTypeId"] = articleTypeId
		c.Data["articleTypeId"] = utils.Atoi64(articleTypeId)
	} else {
		c.Data["articleTypeId"] = -1
	}
	p["curPage"] = curPage
	p["perCount"] = perCount
	totalCount, recordList := service.GetArticleList(p)
	paginator := utils.NewPaginator(int(totalCount), perCount, symPageCount, curPage)
	c.Data["paginator"] = paginator.GetPageHtml()
	c.Data["recordList"] = recordList
	p = map[string]interface{}{}
	_, articleTypeList := service.GetArticleTypeList(p)
	c.Data["articleTypeList"] = articleTypeList
	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "admin/article/list.html"
}